//
//  PhotoScrollView.m
//  Movie
//
//  Created by mycomputer on 15/7/23.
//  Copyright (c) 2015年 杨韦智. All rights reserved.
//

#import "PhotoScrollView.h"
#import "UIImageView+WebCache.h"

@implementation PhotoScrollView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _createSubView];
    }
    return self;
}

- (void)_createSubView {
    
    _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_imageView];
    
    self.maximumZoomScale = 3;
    self.minimumZoomScale = 1;
    
    self.delegate = self;
    
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction)];
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.numberOfTouchesRequired = 1;
    
    [self addGestureRecognizer:doubleTap];
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction)];
    [singleTap requireGestureRecognizerToFail:doubleTap];
    
    [self addGestureRecognizer:singleTap];
    
}

- (void)doubleTapAction {
    NSLog(@"doubleTap");
    if (self.zoomScale == 1) {
        [self setZoomScale:3 animated:YES];
    }else {
        [self setZoomScale:1 animated:YES];
    }
}

- (void)singleTapAction {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"HideNavigationBarNotification" object:nil];
}

- (void)setImageUrl:(NSString *)imageUrl {
    _imageUrl = imageUrl;
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    NSURL *url = [NSURL URLWithString:self.imageUrl];
    [_imageView sd_setImageWithURL:url];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return _imageView;
}


@end
